package com.cskaoyan.gateway.controller.pay;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.pay.PayService;
import com.mall.pay.dto.PayCodeRequest;
import com.mall.pay.dto.PayCodeResponse;
import com.mall.pay.dto.QueryPayResponse;
import com.mall.pay.dto.QueryPayStatusRequest;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author
 * @date 2020/12/24 17:37
 */
@Slf4j
@RestController
@RequestMapping("/cashier")
public class CashierController {
    @Reference(timeout = 3000,check = false)
    PayService payService;
    @Value("${imagePath}")
    String imagePath;

    @PostMapping("pay")
    public ResponseData getPayCode(@RequestBody PayCodeRequest request, HttpServletRequest servletRequest){
        PayCodeResponse payCode = payService.getPayCode(request);
        if (!payCode.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(payCode.getMsg());
        }
        String imgPath = imagePath + payCode.getQrCode();
        return new ResponseUtil<>().setData(imgPath);
    }
    @GetMapping("queryStatus")
    public ResponseData queryStatus(String orderId){
        QueryPayStatusRequest request = new QueryPayStatusRequest();
        request.setOrderId(orderId);
        QueryPayResponse response = payService.queryStatus(request);
        if (!response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil<>().setData(response.getMsg());

    }

}
